<?php
/**
 * Created by PhpStorm.
 * User: tangmusen
 * Date: 2017/10/30
 * Time: 11:08
 */
namespace app\home\controller;

use think\Config;
use think\Controller;
use think\Db;
use think\Request;

class Recharge extends Base
{
    /**
     * 充值1
     */
    public function index(){

        return view();
    }

    /**
     * 充值2
     */
    public function recharge_list(){

        return view();
    }
    /**
     * 充值3
     */
    public function recharge_set(Request $request){

        $uid = session('uid');

        $true_name = Db::name('member')
            ->where('id',$uid)
            ->value('true_name');
        $way = $request->param('way');


        return view('',[
            'way'=>$way,
            'true_name'=>$true_name
        ]);
    }

    /**
     * 微信充值二维码
     */
    public function qrcode_sao(Request $request){
        $uid = session('uid');
        $type = $request->param('type');
        $w['type']=$type;
        $data = Db::name('qrcode')
            ->where($w)
            ->select();
        $key = array_rand($data);
        $data = $data[$key];
        $data['qrcode'] =  Config::get('img_url').$data['qrcode'];
        if($type==1){
            $username = Db::name('member')->where('id',$uid)->value('wx');
        }
        if($type==2){
            $username = Db::name('member')->where('id',$uid)->value('alipay');
        }
        return view('',[
            'data'=>$data,
            'way'=>$type,
            'username'=>$username
        ]);
    }

    /**
     * 微信充值二维码
     */
    public function qrcode(Request $request){
        $uid = session('uid');
        $type = $request->param('type');
        $w['type']=$type;
        $data = Db::name('qrcode')
            ->where($w)
            ->select();
        $key = array_rand($data);
        $data = $data[$key];
        $data['qrcode'] =  Config::get('img_url').$data['qrcode'];
        if($type==4){
            $username = Db::name('member')->where('id',$uid)->value('wx');
        }
        if($type==2){
            $username = Db::name('member')->where('id',$uid)->value('alipay');
        }
        return view('',[
            'data'=>$data,
            'way'=>$type,
            'username'=>$username
        ]);
    }
    /**
     * 支付宝充值二维码
     */
    public function qrcode_pay(Request $request){
        $uid = session('uid');
        $type = $request->param('type');
        $w['type']=$type;
        $data = Db::name('qrcode')
            ->where($w)
            ->select();
        $key = array_rand($data);
        $data = $data[$key];
        $data['qrcode'] =  Config::get('img_url').$data['qrcode'];
        if($type==1){
            $username = Db::name('member')->where('id',$uid)->value('wx');
        }
        if($type==2){
            $username = Db::name('member')->where('id',$uid)->value('alipay');
        }
        return view('',[
            'data'=>$data,
            'way'=>$type,
            'username'=>$username
        ]);
    }
    /**
     * 银行卡充值
     */
    public function recharge_card(){
        $uid = session('uid');
        $w['type']=3;
        $data = Db::name('qrcode')
            ->where($w)
            ->select();
        $key = array_rand($data);
        $data = $data[$key];
        $bank =  Db::name('qrcode')->field('id,k_bank as bank')->where('type',3)->select();
        $true_name = Db::name('member')->where('id',$uid)->value('true_name');
        return view('',[
            'data'=>$data,
            'bank'=>$bank,
            'true_name'=>$true_name
        ]);

    }
    /**
     * 获取一家银行
     */
    public function get_bank_one(Request $request){
        $k_bank = $request->post('bank');
        $w['k_bank']=$k_bank;
        $data = Db::name('qrcode')
            ->where($w)
            ->find();
        json_return(200,'成功',$data);

    }

    /**
     * 线下充值
     */
    public function offline_charge(Request $request){
        $uid      = session('uid');
        $money    = $request->post('money');
        $name     = $request->post('name');
        $remark   = $request->post('remark');
        $way      = $request->post('way');
        if(!$uid ||!$money||!$name ||!$remark ||!$way){
            json_return(201,'网络错误!');
        }
        if($way==1){
           $up['wx']     = $name;
        }
        if($way==2){
            $up['alipay'] = $name;
        }
        if($way==3){
            $up['true_name'] = $name;
        }
        Db::name('member')->where('id',$uid)->update($up);
        $charge['way']    = $way;
        $charge['uid']    = $uid;
        $charge['money']  =  $money;
        $charge['name']   = $name;
        $charge['type']   = 2;
        $charge['remark'] = $remark;
        $charge['create_at'] = time();
        $result = Db::name('recharge')->insert($charge);
        if($result){
            json_return(200,'提交成功,请等待后台审核!');
        }else{
            json_return(201,'提交失败');
        }

    }



}